package com.example.jiaju.ui.home;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.jiaju.DataApplication;
import com.example.jiaju.R;
import com.example.jiaju.adapter.NewHouseAdapter;
import com.example.jiaju.api.HouseApi;
import com.example.jiaju.entity.House;
import com.example.jiaju.ui.main.HouseActivity;
import com.example.jiaju.utils.MyItemDecoration;
import com.example.jiaju.utils.RetrofitUtils;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BuildHomeFragment extends Fragment {


    private List<House.PageDTO.ListDTO> list;
    private RecyclerView recyclerView;
    private NewHouseAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_build_home, container, false);

        //网络请求三步走
        //1.获取到API接口
        HouseApi houseApi= RetrofitUtils.getRetrofit(DataApplication.HTTP_URL).create(HouseApi.class);
        //2.获取到实体类
        Call<House> call=houseApi.getAllHouse(1,5);
        //3.消息请求队列，取出数据
        call.enqueue(new Callback<House>() {
            @Override
            public void onResponse(Call<House> call, Response<House> response) {
                House house=response.body();
                list=house.page.list;
                recyclerView=view.findViewById(R.id.build_home_recyclerview);
                adapter=new NewHouseAdapter(getActivity(),list);
                recyclerView.setAdapter(adapter);
                recyclerView.addItemDecoration(new MyItemDecoration(4));
                recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
            }

            @Override
            public void onFailure(Call<House> call, Throwable t) {

            }
        });

        return view;
    }
}